package leetcode_2301_2400;

public class LeeCode_2397 {
    public static void main(String[] args) {
        System.out.println(maximumRows(new int[][]{{0, 0, 1}, {1, 0, 0}, {0, 0, 0}}, 2));
    }
    private static int maximumRows(int[][] matrix, int numSelect) {
        int m = matrix.length, n = matrix[0].length;
        int[] tab = new int[m];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (matrix[i][j] == 1){
                    tab[i] += Math.pow(2, n - 1 - j);
                }
            }
        }
        int max = (1 << n) - 1, ans = 0;
        for (int i = max; i > 0; i = (i - 1) & max){
            if (Integer.bitCount(i) == numSelect){
                int c = 0;
                for (int j = 0; j < m; j++) {
                    if (((i | tab[j]) ^ i) == 0)
                        c++;
                }
                ans = Math.max(ans, c);
            }
        }
        return ans;
    }
}